import 'package:flutter/material.dart';

import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';

class KqReasonDialog extends Dialog {
  //标题
  final String title;

  ///每一项的
  final String Function(int index)? renderTitleCallback;

  /// 个数
  final int? totalCount;

  //点击了哪一项
  final Function(int index)? callBack;

  /// 网格最大显示行数，超过则滚动
  final int maxRowForScroll;

  final double? dialogWidth;

  const KqReasonDialog(
      {super.key,
      required this.title,
      required this.renderTitleCallback,
      required this.totalCount,
      this.dialogWidth,
      this.maxRowForScroll = 6,
      this.callBack});

  Widget renderItemWidget(int index) {
    String text = renderTitleCallback!(index);
    return InkWell(
      onTap: () {
        if (callBack != null) {
          callBack!(index);
        }
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2.0),
          color: KqThemeManager.getCommonConfig().itemBgColor ??
              KqThemeColors.bgF4,
        ),
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(horizontal: 5),
        child: Text(
          text,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
              color: KqThemeManager.getDialogConfig().titleColor ??
                  KqThemeColors.text59,
              fontSize: 14),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    int total = totalCount!;
    double scale = (dialogWidth ?? 320) / 320;
    return Center(
      child: Container(
        width: 320 * scale,
        padding: const EdgeInsets.only(bottom: 16),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8.0),
          color: KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
              KqThemeColors.bgWhite,
        ),
        constraints: BoxConstraints(
            maxHeight: total <= 0
                ? (48 * scale) + 58 + 16
                : (total / 3.0).ceil() >= maxRowForScroll
                    ? (48 * scale) * maxRowForScroll + 58 + 16
                    : (48 * scale) * (total / 3.0).ceil() + 58 + 16),
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(
                  top: 24, bottom: 16, left: 16, right: 16),
              child: Text(
                title,
                maxLines: 1,
                style: TextStyle(
                    fontSize: 16,
                    color: KqThemeManager.getDialogConfig().titleColor ??
                        KqThemeColors.text26,
                    fontWeight: FontWeight.w600),
              ),
            ),
            Expanded(
              child: GridView.builder(
                shrinkWrap: true,
                padding: const EdgeInsets.only(left: 15, right: 15),
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 7,
                  mainAxisSpacing: 8,
                  childAspectRatio: 92 / 33.0,
                ),
                itemCount: total,
                itemBuilder: (BuildContext context, int index) {
                  return renderItemWidget(index);
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
